<template>
  <div id="aps">
    <div class="bigBox">
      <div class="bg">
        <!--面包屑-->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>全部商品</el-breadcrumb-item>
          <el-breadcrumb-item v-show="!search">分类</el-breadcrumb-item>
          <el-breadcrumb-item v-show="search">搜索</el-breadcrumb-item>
          <el-breadcrumb-item v-show="search">{{ search }}</el-breadcrumb-item>
        </el-breadcrumb>
        <!--标题-->
        <div class="nav">
          <div class="pronav">
            <div class="title">分类</div>
            <el-tabs v-model="active" type="card">
              <el-tab-pane
                v-for="item in tagsList"
                :key="item.category_id"
                :label="item.category_name"
                :name="'' + item.category_id"
              ></el-tab-pane>
            </el-tabs>
          </div>
          
        </div>
      </div>
      <div class="listBox">
        <mi-list :mobellist="product" :isFlag="true"></mi-list>
        <!--分页-->
        <div class="pages">
          <el-pagination
            background
            layout="prev, pager, next"
            :total="total"
            :page-size="pageSize"
            @current-change="currentChange"
          >
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import allShopAPI from "../../api/allShop";
export default {
  components: {},
  name: "",
  data() {
    return {
      categoryID: [1],
      currentPage: 1,
      pageSize: 15,
      total: 0,

      tagsList: [], //tags
      product: [], //list   ALL
      active: "0", //获取点击下标
      search: "", //搜索框数据
    };
  },
  activated() {
    this.active = "-1";

  },
  created() {
    this.getLcData();
    this.getShopList(); //获取tags数据
    this.getShop("getAllProduct"); //获取单条数据数据  传递的参数是api方法名字

    
  },
  methods: {
    //tags标题--分类列表
    async getShopList() {
      let { data: res } = await allShopAPI.getShopList();
      // console.log(res.category);
      res.category.unshift({
        category_id: 0,
        category_name: "全部",
      });
      this.tagsList = res.category;
    },

    //商品数据
    async getShop(apis = "getProductByCategory") {
      let allList = {
        categoryID: this.categoryID,
        currentPage: this.currentPage,
        pageSize: this.pageSize,
      };
      let { data: res } = await allShopAPI[apis](allList);
      // console.log(res);
      this.product = res.Product;
      this.total = res.total;
    },

    //分页页码变动时调用
    currentChange(currentPage) {
      this.currentPage = currentPage;
    },

    //更多列表
    async getLcData() {
      this.categoryID = this.$route.query;
      let arr = [];
      for (var i in this.categoryID) {
        // console.log(this.categoryID[i])
        arr.push(this.categoryID[i]);
      }
      let allList = {
        categoryID: arr,
        currentPage: this.currentPage,
        pageSize: this.pageSize,
      };
      let { data: res } = await allShopAPI.getLcData(allList);
      console.log(res);
      this.product = res.Product;
      this.total = res.total;
    },

    //搜索数据
    async getSearch() {
      let arr = {
        search: this.search,
        currentPage: this.currentPage,
        pageSize: this.pageSize,
      };
      let { data: res } = await allShopAPI.getSearch(arr);
      // console.log(arr);
      this.product = res.Product;
      this.total = res.total;
    },
  },
  watch: {
    active(val) {
      //分类切换
      console.log(val);
      //赋值动态id
      let categoryID = Number(val);
      this.categoryID = [categoryID];

      //重置分类分页数据
      this.currentPage = 1;
      this.total = 0;
      this.search = "";

      //如果下标为0的话就渲染全部商品数据
      if (categoryID === 0) {
        //全部商品
        this.categoryID = [];
        this.getShop("getAllProduct");
      } else {
        this.getShop();
      }
    },
    //点击分页导航的时候
    currentPage(currentPage) {
      this.getShop("getAllProduct");
    },
    //监听路由
    $route(value) {
      console.log(value.query);
      if (value.query.search) {
        this.search = value.query.search;
        this.getSearch();
      }
    },
  },
};
</script>

<style lang="scss" scoped>
#aps {
  width: 100%;
  height: 1138px;
  background-color: #dadada;
  .bigBox {
    width: 100%;
    height: 85px;
    background: white;
    margin-bottom: 20px;
    .bg {
      width: 1225px;
      height: 80px;
      margin: 0 auto;
    }
    .el-breadcrumb {
      padding: 20px 20px;

      font-size: 16px;
      transition: color 0.2s cubic-bezier(0.645,  0.045,  0.355,  1);
      color: #303133;
      line-height: 30px;
    }
  }
  .nav {
    background-color: white;
    .pronav {
      width: 1225px;
      height: 40px;
      line-height: 40px;
      margin: 0 auto;
      .title {
        width: 50px;
        font-size: 16px;
        font-weight: 700;
        float: left;
      }
    }
  }

  .listBox {
    width: 1225px;
    min-height: 650px;
    margin: 10px auto;
    padding: 10px 0;
    .pages {
      height: 50px;
      text-align: center;
    }
  }
}
</style>